/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"54DF80F7-2753-448D-B9DE-058CA85C5921",variableType:4}
 */
var $bloqueado = 1;

/**
 * @properties={typeid:35,uuid:"5BE227B7-E266-4557-8357-6880634645B0",variableType:-4}
 */
var $menu = plugins.window.createPopupMenu();

/**
 * Index actual del registros
 * @author Roberto J. Blasco
 * @version 1.0
 * @type {Number}
 * @properties={typeid:35,uuid:"DD1BC356-271F-4F1C-8542-6B54F1C32E49",variableType:4}
 */
var $n = 0;

/**
 * Número total de registros en el foundset
 * @author Roberto J. Blasco
 * @version 1.0
 * @type {Number}
 * @properties={typeid:35,uuid:"18D5D726-D001-426B-AE56-FEABDD9F3C79",variableType:4}
 */
var $m = 0;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"CF304465-E6B2-47E9-96F3-1D9BA04C3B41"}
 */
var $texto_busqueda = "";

/**
 * TODO generated, please specify type and doc for the params
 * @param event
 *
 * @properties={typeid:24,uuid:"5011E696-BD98-43AA-B5A7-FDC54BD87DCF"}
 */
function _buscar(event){
	
	if (forms[controller.getName()].buscar){
		forms[controller.getName()].buscar(event);
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"9D37A451-395D-4C6F-8463-ACFCC8CD7A1B"}
 */
function _alta(event) {
		
	var nombre_tabla = databaseManager.getDataSourceTableName(forms[event.getFormName()].foundset.getDataSource());
	var frm_alta = "frm_" + nombre_tabla;
	
	if (forms[controller.getName()].alta_antes){
		forms[controller.getName()].alta_antes(event);
	}
	
	forms[frm_alta].foundset.newRecord();
	if (forms[frm_alta].foundset.getSelectedRecord().isNew()){
		if (forms[frm_alta].$formulario_padre != null){
			forms[frm_alta].$formulario_padre = event.getFormName();
		}
	}
	forms[frm_alta]._doEdit();
	
	forms.pigeon_menu_navigator.controller.enabled = false;
	
	forms[globals.$frm_siv_main].elements.tab_principal.removeAllTabs();
	forms[globals.$frm_siv_main].elements.tab_principal.addTab(forms[frm_alta]);
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"4E5C7CCB-B44E-42A3-A0DA-DDB769ECBF2A"}
 */
function _onShow(firstShow, event) {
	
	bloquear_foundset(event);
	
	if (forms[controller.getName()].onShow){
		forms[controller.getName()].onShow(firstShow, event);
	}
	
	if (foundset.getSize() == 0){
		$n = 0;
		$m = 0;
	}else{
		$n = foundset.getSelectedIndex();
		$m = databaseManager.getFoundSetCount(foundset);
	}
}

/**
 * Ir Formulario
 * @author Roberto J. Blasco
 * @version 1.0
 * @param {JSEvent} event the event that triggered the action
 * @properties={typeid:24,uuid:"C81A3041-FA41-407A-AA1A-B5D96210424C"}
 */
function _ir_formulario(event) {

	var nombre_tabla = databaseManager.getDataSourceTableName(foundset.getSelectedRecord().foundset.getDataSource());
	var frm = "frm_" + nombre_tabla;
	globals.ir_formulario(event, frm,foundset,globals.$MODO_LECTURA);
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"6E16CB34-94C6-4CBA-8234-83D55E096AD4"}
 */
function _eliminar(event) {

	foundset.deleteRecord(foundset.getSelectedRecord());
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"7E046603-7FA2-4EAD-B52C-E7FF61421C0A"}
 */
function _menu(event) {

	var submenu_alta_registro = $menu.addMenuItem("Alta de un Registro", alta_registro);
	submenu_alta_registro.setIcon("media:///iconos/ic_add_box_white_18dp.png");
	submenu_alta_registro.methodArguments = [event];
	
	var submenu_busqueda_avanzada = $menu.addMenuItem("Búsqueda Avanzada", _busqueda_avanzada);
	submenu_busqueda_avanzada.setIcon("media:///iconos/ic_find_replace_white_18dp.png");
	submenu_busqueda_avanzada.methodArguments = [event];
	
	$menu.addSeparator();
	
	var submenu_desbloquear_listado = $menu.addMenuItem("Desbloquear Listado", null);
	submenu_desbloquear_listado.setIcon("media:///iconos/ic_lock_open_white_18dp.png");
	submenu_desbloquear_listado.methodArguments = [event];
	
	var submenu_bloquear_listado = $menu.addMenuItem("Bloquear Listado", null);
	submenu_bloquear_listado.setIcon("media:///iconos/ic_lock_outline_white_18dp.png");
	submenu_bloquear_listado.methodArguments = [event];

	if (forms[controller.getName()].menu){
		forms[controller.getName()].menu(event);
	}
	
	if (event.getSource()) {
		$menu.show(event.getSource());
	}
}

/**
 * @param {JSEvent} event
 *
 * @properties={typeid:24,uuid:"CBAC5ADB-1071-4FDF-B01D-CC1FE40C3875"}
 */
function alta_registro(event){
	
}

/**
 * @param {JSEvent} event
 *
 * @properties={typeid:24,uuid:"75C5163B-48BE-471D-AD6E-346211196F7D"}
 */
function _busqueda_avanzada(event){
	
}

/**
 * Callback method when form is (re)loaded.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"2B12BF46-2352-4771-ABEE-656E5FC68AB6"}
 * @AllowToRunInFind
 */
function _onLoad(event) {

	foundset.multiSelect = true;
	
	var nombre_tabla = databaseManager.getDataSourceTableName(controller.getDataSource());
	/** @type {JSFoundSet<db:/pigeon_board/adm_t900_tablas>} */
	var fs_adm_t900_tablas = databaseManager.getFoundSet(scopes['adm_t900_tablas'].$NOMBRE_SERVIDOR,scopes['adm_t900_tablas'].$NOMBRE_TABLA);
	if (fs_adm_t900_tablas.find()){
		fs_adm_t900_tablas.s_nombre_tabla = nombre_tabla;
		fs_adm_t900_tablas.search();
	}
	
	if (fs_adm_t900_tablas.getSize()){
		elements.lbl_header_txt.text = fs_adm_t900_tablas.s_descripcion_lst.toUpperCase();
		elements.lbl_icono.imageURL = "media:///iconos/" + fs_adm_t900_tablas.s_icono_claro;
	}else{
		application.output("No se ha encontrado la descripción del listado para la tabla " + nombre_tabla, LOGGINGLEVEL.DEBUG);
	}
}



/**
 * Called before the form component is rendered.
 *
 * @param {JSRenderEvent} event the render event
 *
 * @properties={typeid:24,uuid:"9CB1E822-50B9-43C5-BA35-FA5954BD4D0D"}
 */
function onRender_check(event) {

	if (event.isRecordSelected()) {
		event.getRenderable().imageURL = "media:///iconos/ic_check_black_18dp.png";
		event.getRenderable().bgcolor = "#D6EAD6";
	} else {
		event.getRenderable().imageURL = "media:///iconos/ic_crop_3_2_grey600_18dp.png";
		event.getRenderable().bgcolor = "#fff";
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"CBC14BAB-0417-4C31-9067-9B97F86B21BF"}
 */
function onAction(event) {
	// TODO Auto-generated method stub
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"480D6490-31C1-4DDE-A4CF-694C78E9C507"}
 */
function seleccionar_foundset(event) {

	var fs_size = databaseManager.getFoundSetCount(foundset);
	for (var x = 1; x <= fs_size; x++){
		
		var pk = foundset.getRecord(x).getPKs()[0];
		foundset.selectRecord(pk);
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"1416F91F-FCA8-4AAD-81E0-9B858CD303A9"}
 */
function bloquear_foundset(event) {
	
	var todos_nombres = forms[controller.getName()].elements.allnames;
	for (var x = 0; x < todos_nombres.length; x++){
		if (todos_nombres[x].indexOf('lbl_') >= 0) {
			forms[controller.getName()].elements[todos_nombres[x]].visible = true;
		}else if (todos_nombres[x].indexOf('fld_') >= 0) {
			forms[controller.getName()].elements[todos_nombres[x]].visible = false;
		}
	}
	
	elements.btn_desbloquear.visible = true;
	elements.btn_bloquear.visible = false;
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"D7D77906-A613-4476-9F42-0451ACBBDEC0"}
 */
function desbloquear_foundset(event) {
	
	var todos_nombres = forms[controller.getName()].elements.allnames;
	for (var x = 0; x < todos_nombres.length; x++){
		if (todos_nombres[x].indexOf('lbl_') >= 0) {
			forms[controller.getName()].elements[todos_nombres[x]].visible = false;
		}else if (todos_nombres[x].indexOf('fld_') >= 0) {
			forms[controller.getName()].elements[todos_nombres[x]].visible = true;
		}
	}
	
	elements.btn_desbloquear.visible = false;
	elements.btn_bloquear.visible = true;
}
